package com.aysafety.iotdataplatformclient.microseism.services.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.aysafety.core.DBDriver.paradox.ParadoxDriver;
import com.aysafety.core.common.DateUtils;
import com.aysafety.core.model.MicroseismModel;
import com.aysafety.iotdataplatformclient.microseism.dao.MicroseismDao;
import com.aysafety.iotdataplatformclient.microseism.services.MicroseismService;
import com.aysafety.iotdataplatformclient.modelConfigurer.MicroseismModelConfiger;



@Service
public class MicroseismServiceImpl implements MicroseismService {

    @Autowired
    MicroseismModelConfiger microseismModelConfiger;

    @Autowired
    MicroseismDao microseismDao;

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public MicroseismModel transformData(String dataString) {
        Map<String, String> structure = microseismModelConfiger.getDatakey();
        Map<String, Object> temp = JSON.parseObject(dataString, Map.class);
        MicroseismModel microseismModel = this.transformData(temp, structure);
        return microseismModel;
    }

    private MicroseismModel transformData(Map<String, Object> temp, Map<String, String> structure) {
        MicroseismModel microseismModel = new MicroseismModel();
        for (String key : structure.keySet()) {
            String columnKey = structure.get(key);
            if ("EntId".equals(key)) {
                microseismModel.setEntId(microseismModelConfiger.getEntId());
            }
            if ("area".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setArea((String) temp.get(columnKey));
                }
            }
            if ("cause".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setCause((String) temp.get(columnKey));
                }
            }
            if ("emergoa".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setEmergoa(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("poklad".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setPoklad(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("type".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setType((String) temp.get(columnKey));
                }
            }
            if ("datetime".equals(key)) {
                String[] columnTemp = columnKey.split("\\+");
                String dateTimeStr = "";
                for (String value : columnTemp) {
                    if (temp.containsKey(value)) {
                        dateTimeStr += temp.get(value) + " ";
                    }
                }
                dateTimeStr = dateTimeStr.trim();
                microseismModel.setDatetime(DateUtils.fomatDateTime(dateTimeStr));
            }
            if ("workingface".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setWorkingFace((String) temp.get(columnKey));
                }
            }
            if ("x".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setX(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("y".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setY(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
            if ("z".equals(key) && temp.containsKey(columnKey)) {
                if (null != temp.get(columnKey)) {
                    microseismModel.setZ(Double.valueOf(temp.get(columnKey).toString()));
                }
            }
        }
        return microseismModel;
    }

    public MicroseismModel readData(String jdbcPath, String charset, String locale, String tableId) throws Exception {
        String connKey = ParadoxDriver.connection(jdbcPath, charset, locale);
        Map<String, String> structure = microseismModelConfiger.getDatakey();
        Map<String, Object> temp = ParadoxDriver.lastData(tableId, "", connKey, structure);
        ParadoxDriver.close(connKey);
        MicroseismModel microseismModel = this.transformData(temp, structure);
        return microseismModel;
    }

    public MicroseismModel readData() throws Exception {
        Map<String, String> structure = microseismModelConfiger.getDatakey();
        Map<String, Object> temp = microseismDao.lastData();
        MicroseismModel microseismModel = this.transformData(temp, structure);
        return microseismModel;
    }

    public int readHistroyDataCount(Map<String, Object> conditions) {
        return microseismDao.readHistroyDataCount(conditions);
    }

    public List<MicroseismModel> readHistroyData(Map<String, Object> conditions, int start, int prePage) {
        List<MicroseismModel> readHistroyDataModel = new ArrayList<MicroseismModel>();
        Map<String, String> structure = microseismModelConfiger.getDatakey();
        List<Map<String, Object>> readHistroyDatas = microseismDao.readHistroyData(conditions, start, prePage);
        for (Map<String, Object> readHistroyData : readHistroyDatas) {
            MicroseismModel microseismModel = this.transformData(readHistroyData, structure);
            readHistroyDataModel.add(microseismModel);
        }
        return readHistroyDataModel;
    }
}
